package com.yiya.ms.action;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.yiya.bean.Product;
import com.yiya.bean.SysUser;
import com.yiya.model.PriceRelModel;
import com.yiya.model.ProductCplex;
import com.yiya.model.ProductModel;
import com.yiya.service.ProductService;
import com.yiya.utils.HtmlUtil;
import com.yiya.utils.SessionUtils;

@Controller
@RequestMapping("/product")
public class ProductAction extends BaseAction {
	@SuppressWarnings("unused")
	private final static Logger log = Logger.getLogger(ProductAction.class);

	@Autowired
	private ProductService productService;

	@RequestMapping("/product_list")
	public ModelAndView productList(HttpServletRequest request) {
		Map<String, Object> context = getRootMap();
		return forword("product/product", context);
	}

	@RequestMapping("/material_list")
	public ModelAndView materialList(HttpServletRequest request) {
		Map<String, Object> context = getRootMap();
		return forword("raw_material/raw_material", context);
	}

	@RequestMapping("/dataList")
	public void datalist(ProductModel model, HttpServletResponse response)
			throws Exception {
		List<ProductCplex> products = productService.queryByList(model);
		int count = productService.queryByCount(model);
		// 设置页面数据
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		jsonMap.put("rows", products);
		jsonMap.put("total", count);
		HtmlUtil.writerJson(response, jsonMap);
	}

	@RequestMapping("/save")
	public void save(Product bean, HttpServletRequest request,
			HttpServletResponse response) {
		Date nowTime = new Date();
		SysUser user = SessionUtils.getUser(request);
		ProductModel productModel = new ProductModel();
		if (bean.getId() == null) {
			productModel.setCode(bean.getCode());
			Product product = productService.queryByCode(productModel);
			if (product == null) {
				// 如果产品编号还不存在，可以录入
				bean.setCreateTime(nowTime);
				bean.setCreateBy(user.getId());
				productService.add(bean);
				sendSuccessMessage(response, "保存成功");
			} else {
				// 如果产品编号已存在，不可录入
				sendFailureMessage(response, "已有编号为:" + bean.getCode()
						+ "的产品，编号不可重复!");
			}
		} else {
			productModel.setId(bean.getId());
			productModel.setCode(bean.getCode());
			List<Product> products = productService.queryOther(productModel);
			if(products==null||products.size()==0){
				bean.setUpdateBy(user.getId());
				bean.setUpdateTime(nowTime);
				productService.Update(bean);
				sendSuccessMessage(response, "保存成功");
			}else{
				// 如果产品编号已存在，不可录入
				sendFailureMessage(response, "已有编号为:" + bean.getCode()
						+ "的产品，编号不可重复!");
			}
		}
	}

	@RequestMapping("/getId")
	public void getId(Integer id, HttpServletResponse response) {
		Map<String, Object> context = getRootMap();
		Product product = productService.queryById(id);
		if (product == null) {
			sendFailureMessage(response, "没有找到对应的记录!");
			return;
		}
		context.put(SUCCESS, true);
		context.put("data", product);
		HtmlUtil.writerJson(response, context);
	}

	@RequestMapping("/delete")
	public void delete(Integer[] id, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		SysUser user = SessionUtils.getUser(request);
		productService.delete(id, user);
		sendSuccessMessage(response, "删除成功");
	}

	@RequestMapping("/loadProdList")
	public void loadProdList(ProductModel model, HttpServletResponse response)
			throws Exception {
		List<Product> products = productService.queryAllList(model);
		HtmlUtil.writerJson(response, products);
	}

	@RequestMapping("/loadProdListByMerchant")
	public void loadProdListByMerchant(PriceRelModel model,
			HttpServletResponse response) throws Exception {
		List<Product> products = productService.queryAllListByMerchant(model);
		HtmlUtil.writerJson(response, products);
	}
}
